﻿@model GiftCardListModel
@using Telerik.Web.Mvc.UI
@{
    var gridPageSize = EngineContext.Current.Resolve<IT.CMS.Core.Domain.Common.AdminAreaSettings>().GridPageSize;

    //page title
    ViewBag.Title = T("Admin.GiftCards").Text;
}
@using (Html.BeginForm())
{
    <div class="section-header">
        <div class="title">
            <img src="@Url.Content("~/Areas/Admin/content/images/ico-sales.png")" alt="" />
            @T("Admin.GiftCards")
        </div>
        <div class="options">
            <a href="@Url.Action("Create")" class="t-button">@T("Admin.Common.AddNew")</a>
        </div>
    </div>
    <table width="100%">
        <tr>
            <td class="adminTitle">
                @Html.CmsLabelFor(model => model.ActivatedId):
            </td>
            <td class="adminData">
                @Html.DropDownListFor(model => model.ActivatedId, Model.ActivatedList, new { @class = "adminInput" })
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.CmsLabelFor(model => model.CouponCode):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.CouponCode)
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="button" id="btnSearch" class="t-button" value="@T("Admin.Common.Search")" />
            </td>
        </tr>
    </table>
    <p>
    </p>
    <table class="adminContent">
        <tr>
            <td>
                @(Html.Telerik().Grid<GiftCardModel>()
                        .Name("giftcards-grid")
                .ClientEvents(events => events.OnDataBinding("onDataBinding"))
                        .Columns(columns =>
                        {
                            columns.Bound(x => x.AmountStr);
                            columns.Bound(x => x.RemainingAmountStr);
                            columns.Bound(x => x.GiftCardCouponCode);
                            columns.Bound(x => x.IsGiftCardActivated)
                                .Template(
                                    @<img alt="" src="@Url.Content("~/Areas/Admin/content/images/active-" + item.IsGiftCardActivated.ToString().ToLowerInvariant() + ".gif")" />
                                )
                                .ClientTemplate("<img alt='' src='" + Url.Content("~/Areas/Admin/content/images/") + "active-<#= IsGiftCardActivated #>.gif' />")
                                .Centered()
                                .Width(100);
                            columns.Bound(x => x.CreatedOn);
                            columns.Bound(x => x.Id)
                                .Template(x => Html.ActionLink(T("Admin.Common.Edit").Text, "Edit", new { id = x.Id }))
                                .ClientTemplate("<a href=\"Edit/<#= Id #>\">" + T("Admin.Common.Edit").Text + "</a>")
                                .Width(50)
                                .Centered()
                                .HeaderTemplate(T("Admin.Common.Edit").Text)
                                .Filterable(false);
                        })
                                                .Pageable(settings => settings.PageSize(gridPageSize).Position(GridPagerPosition.Both))
                                                .DataBinding(dataBinding => dataBinding.Ajax().Select("GiftCardList", "GiftCard", Model))
                                                .EnableCustomBinding(true))
            </td>
        </tr>
    </table>
    
    <script type="text/javascript">

        $(document).ready(function () {

            $('#btnSearch').click(function () {
                var grid = $('#giftcards-grid').data('tGrid');
                grid.currentPage = 1; //new search. Set page size to 1
                grid.ajaxRequest();
                return false;
            });
        });

        $("#@Html.FieldIdFor(model => model.CouponCode)").keydown(function (event) {
            if (event.keyCode == 13) {
                $("#btnSearch").click();
                return false;
            }
        });
        
        function onDataBinding(e) {
            var searchModel = {
                ActivatedId: $('#@Html.FieldIdFor(model => model.ActivatedId)').val(),
                CouponCode: $('#@Html.FieldIdFor(model => model.CouponCode)').val()
            };
            e.data = searchModel;
        }

    </script>
    
}